package com.cn.system.controller;

import com.cn.common.annotation.Log;
import com.cn.common.controller.BaseController;
import com.cn.common.domain.FileDO;
import com.cn.common.domain.Tree;
import com.cn.common.service.FileService;
import com.cn.common.utils.MD5Utils;
import com.cn.common.utils.ResponseUtil;
import com.cn.common.utils.ShiroUtils;
import com.cn.system.domain.MenuDO;
import com.cn.system.service.MenuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@Api(value = "登录",tags = "登录LoginController")
public class LoginController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    MenuService menuService;
    @Autowired
    FileService fileService;
    @GetMapping({"/", ""})
    public String home() {
        return "redirect:/login";
    }

    @GetMapping("/main")
    public String welcome() {
        return "main";
    }

    @Log("请求访问主页")
    @GetMapping({"/index"})
    @ApiOperation(value = "登录系统进入首页", notes = "根据用户角色查询菜单权限")
    public String index(Model model) {
        List<Tree<MenuDO>> menus = menuService.listMenuTree(getUserId());
        model.addAttribute("menus", menus);
        model.addAttribute("name", getUser().getName());
        FileDO fileDO = fileService.get(getUser().getPicId());
        if (fileDO != null && fileDO.getUrl() != null) {
            if (fileService.isExist(fileDO.getUrl())) {
                model.addAttribute("picUrl", fileDO.getUrl());
            } else {
                model.addAttribute("picUrl", "/img/photo_s.jpg");
            }
        } else {
            model.addAttribute("picUrl", "/img/photo_s.jpg");
        }
        model.addAttribute("username", getUser().getName());
        return "index";
    }

    @GetMapping("/login")
    public String login(Model model) {
        return "login";
    }

    @Log("登录")
    @PostMapping("/login")
    @ResponseBody
    public ResponseUtil ajaxLogin(String username, String password) {
        password = MD5Utils.encrypt(username, password);
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            return ResponseUtil.ok();
        } catch (AuthenticationException e) {
            return ResponseUtil.error(e.getMessage());
        }
    }

    @GetMapping("/logout")
    public String logout() {
        ShiroUtils.logout();
        return "redirect:/login";
    }

}
